home *** CD-ROM | disk | FTP | other *** search
/ Network CD 2 / Network CD - Volume 2.iso / programs / internet / tcp / amitcp / amitcp-src-22.lha / AmiTCP-2.2 / src / appl / napsaterm / clip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-07  |  3.6 KB  |  155 lines

  1. RCS_ID_C "$Id: clip.c,v 1.6 1993/07/12 22:51:10 ppessi Exp $";
  2. /* 
  3.  * Clipboard routines for the Amiga version of niftyterm.
  4.  * This code borrows heavily from the Clipboard code given
  5.  * in the AmigaMail tech notes, page XIV - 9.
  6.  * 
  7.  * Original code by Todd Williamson.
  8.  *
  9.  * $Author: ppessi $ $Revision: 1.6 $ $Date: 1993/07/12 22:51:10 $
  10.  */
  11.  
  12. #include "nifty.h"
  13. #include "amiga.h"
  14. #include "exec/io.h"
  15.  
  16. #include <devices/clipboard.h>
  17.  
  18. static char clipopen = FALSE;
  19. static struct MsgPort *cport = NULL;
  20. static struct IOClipReq *creq = NULL;
  21.  
  22. /* Saves lots of assignment statements */
  23. #define SETIO(iob, com, buf, len) \
  24.     (iob)->io_Command = (com); \ 
  25.     (iob)->io_Data = (STRPTR)(buf); (iob)->io_Length = (len)
  26.  
  27. /* Used internally by the other clipboard routines.  Clipboard device is 
  28.  * automatically opened on the first read or write.  Must be closed
  29.  * explicitly.
  30.  */
  31. static int
  32. ClipOpen()
  33. {
  34.     clipopen = clipopen ||
  35.     ((cport = CreateMsgPort()) &&
  36.      (creq = (struct IOClipReq *)CreateIORequest(cport, sizeof(*creq))) &&
  37.      !OpenDevice("clipboard.device", 0, (struct IORequest *)creq, 0));
  38.     /* Free resources if necessary */
  39.     if (!clipopen) {
  40.     ClipClose();
  41.     puts("Could not open clipboard");
  42.     return -1;
  43.     }
  44.  
  45.     return 0;
  46. }
  47.  
  48. /* This will close the clipboard device if it is open.  Although the
  49.  * device is automatically opened on the first read or write, it must
  50.  * be closed explicitly.
  51.  */
  52. void
  53. ClipClose()
  54. {
  55.     if (clipopen) 
  56.         CloseDevice((struct IORequest *)creq);
  57.     clipopen = FALSE;
  58.  
  59.     if (creq)
  60.     DeleteIORequest((struct IORequest *)creq);
  61.     creq = NULL;
  62.  
  63.     if (cport)
  64.     DeleteMsgPort(cport);
  65.     cport = NULL;
  66. }
  67.  
  68. static void
  69. ClipLong(lword)
  70. long *lword;
  71. {
  72.     SETIO(creq, CMD_WRITE, lword, 4);
  73.     DoIO((struct IORequest *)creq);
  74. }
  75.  
  76. /* Implement cutting of a string.  Cuts the string to the clipboard.
  77.  */
  78. int
  79. ClipCut(string)
  80. char *string;
  81. {
  82.     long length, slen;
  83.     int odd;
  84.  
  85.     if (ClipOpen() < 0) 
  86.     return -1;
  87.  
  88.     slen = strlen(string);
  89.     odd = (slen & 1);
  90.     length = slen + odd + 3 * sizeof(LONG);
  91.  
  92.     creq->io_ClipID = 0;
  93.     creq->io_Offset = 0;
  94.     ClipLong((LONG *)"FORM");
  95.     ClipLong(&length);
  96.     ClipLong((LONG *)"FTXT");
  97.     ClipLong((LONG *)"CHRS");
  98.     ClipLong(&slen);
  99.  
  100.     /* We make use of the last NUL at the end of the string */ 
  101.     SETIO(creq, CMD_WRITE, string, slen + odd);
  102.     DoIO((struct IORequest *)creq);
  103.     
  104.     creq->io_Command = CMD_UPDATE;
  105.     DoIO((struct IORequest *)creq);
  106.     return 0;
  107. }
  108.     
  109. /* Pass a buffer and a max size, and it gets filled with the current contents
  110.  * of the clipboard (if the contents is textual). Returns -1 on error, length
  111.  * if successful.  The buffer must be at least 8 characters.
  112.  */
  113. long
  114. ClipPaste(buf, max)
  115. char *buf;
  116. long max;
  117. {
  118.     long length = 0, slen = 0, status = 0;
  119.     
  120.     if (ClipOpen() < 0) 
  121.     return 0;
  122.  
  123.     max--;
  124.     SETIO(creq, CMD_READ, buf, 4);
  125.     creq->io_Offset = 0;
  126.     creq->io_ClipID = 0;
  127.     status -= DoIO((struct IORequest *)creq);
  128.  
  129.     if(!strncmp(buf, "FORM", 4)) {
  130.         SETIO(creq, CMD_READ, &length, 4);
  131.     status -= DoIO((struct IORequest *)creq);
  132.     SETIO(creq, CMD_READ, buf, 8);
  133.     status -= DoIO((struct IORequest *)creq);
  134.     
  135.     if(!strncmp(buf, "FTXTCHRS", 8)) {
  136.         SETIO(creq, CMD_READ, &slen, 4);
  137.         status -= DoIO((struct IORequest *)creq);
  138.         
  139.         slen = MIN(slen, max);
  140.         SETIO(creq, CMD_READ, buf, slen);
  141.         status -= DoIO((struct IORequest *)creq);
  142.         
  143.         /* Force EOF on read */
  144.         SETIO(creq, CMD_READ, 0, 1);
  145.         creq->io_Offset += length;
  146.         do {
  147.           status -= DoIO((struct IORequest *)creq);
  148.         } while (creq->io_Actual);
  149.     }
  150.     }
  151.     if(status) return 0;
  152.     buf[slen] = '\0';
  153.     return slen;
  154. }
  155.